package com.infinitus.activity.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 
* 日期格式转换工具类
*
*/
public class DatetimeFormatUtil
{
	private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	private static final DateFormat DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	private static final DateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
	
	/**
	 * 按照格式 ：yyyy-MM-dd HH:mm:ss 转换
	 * 
	 * @param date
	 * @return
	 */
	public static String datetimeFormat(Date date)
	{
		if (date != null)
		{
			return DATETIME_FORMAT.format(date);
		}
		
		return "";
	}
	
	public static String timeFormat(Date date)
	{
		if (date != null)
		{
			return TIME_FORMAT.format(date);
		}
		
		return "";
	}
	
	/**
	 * 字符串转换成日期类型
	 * @param datetime 格式：yyyy-MM-dd HH:mm:ss
	 * @return
	 *
	 */
	public static Date parseDatetime(String datetime){
		if (datetime != null)
		{
			try {
				return DATETIME_FORMAT.parse(datetime);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}
	
	
	/**
	 * 按照格式 ：yyyy-MM-dd 转换
	 * 
	 * @param date
	 * @return
	 */
	public static String dateFormat(Date date)
	{
		if (date != null)
		{
			return DATE_FORMAT.format(date);
		}
		
		return "";
	}
	


	
	public static Date parseToDate( int dayNum)
	{
		try
		{
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_MONTH, + dayNum);
			return calendar.getTime();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static Date parseToDateMonth(int month) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -month);
		return calendar.getTime();
	}
	
	
	public static Date parseDate(String dateStr) throws ParseException
	{
		return DATE_FORMAT.parse(dateStr);
	}
	

}
